package com.situ.company.dao;

import com.situ.company.entity.Applicant;
import com.situ.company.entity.Dept;
import com.situ.company.entity.Employee;
import com.situ.company.util.DBUtil;
import com.situ.company.util.StringUtil;
import com.situ.company.vo.ApplicantVO;
import com.situ.company.vo.EmployeeVO;
import com.situ.company.vo.TableVO;

import java.util.List;

public class ApplicantDao {
    public void insert(Applicant a){
        String sql="insert into applicant (name,age,sex,education,create_time)values(?,?,?,?,?)";
        DBUtil.executeDML(sql,a.getName(),a.getAge(),a.getSex(),a.getEducation(),a.getCreateTime());
    }

    public List<ApplicantVO> list(Integer page, Integer limit, String name){
        String sql ="select * from applicant ";

        if(StringUtil.isNotBlank(name)){
            sql +=" where instr(name,?)>0 limit ?,?";
            return DBUtil.executeDQL(sql, ApplicantVO.class, name,(page - 1) * limit, limit);
        }
        sql += " limit ?,?";
        return DBUtil.executeDQL(sql, ApplicantVO.class, (page - 1) * limit, limit);
    }


    public void delete(Integer id) {
        String sql ="delete from applicant where id=?";
        DBUtil.executeDML(sql,id);
    }

    public List<Applicant> edit(Integer id) {
        String sql="select * from applicant where id =?";
        return DBUtil.executeDQL(sql,Applicant.class,id);
    }

    public void update(Applicant a) {
        String sql ="update applicant set name=?,age=?,sex=?,education=? where id =?";
        DBUtil.executeDML(sql,a.getName(),a.getAge(),a.getSex(),a.getEducation(),a.getId());
    }

    public Long count(String name) {
        String sql ="select count(1) from applicant";

        if(StringUtil.isNotBlank(name)){
            sql += " where instr(applicant.name,?)>0 ";
            return DBUtil.executeCount(sql, name);
        }

        return DBUtil.executeCount(sql);
    }
}
